import Vue from "vue";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import axios from "axios";
import VueAxios from "vue-axios";
import Cookie from "js-cookie";
import Base from "../src/util/Base64";
import Prosperity from "../src/util/Prosperity";
import echarts from "echarts";
import VueI18n from "vue-i18n";

import * as formatDate from "@/util/time.js";
Vue.use(VueAxios, axios);
Vue.use(VueI18n);
axios.defaults.baseURL = "http://192.168.1.188:8080/"; //接口文档路径
Vue.use(ElementUI);

const i18n = new VueI18n({
  locale: "中文", // 语言标识
// 通过切换locale的值来实现语言切换
  messages: {
    中文: require("./assets/langs/zh.js"), // 中文
    English: require("./assets/langs/en.js") // 英文
  }
});
var perms = Cookie.get("perms");
Vue.directive("power", {
  inserted: function(el, binding) {
    console.log(perms);
    if (!perms.includes(binding.value)) {
      el.remove();
    }
  }
});

Vue.config.productionTip = false;
Vue.prototype.$echarts = echarts;
Vue.prototype.Cookies = Cookie;
Vue.prototype.Base64 = Base;
Vue.prototype.Prosperity = Prosperity;

//全局守卫鉴权进入
router.beforeEach((to, from, next) => {
  var routeName = to.meta[0].name; //即将要去到的页面的name名
  document.title = to.meta[0].name;
  //登录页面不用鉴权
  if ("登录" === routeName) {
    //如果name是Login的就跳转
    next();
  } else {
    var token = Cookie.get("token");
    if (token) {
      next(); //有令牌可以进入
    } else {
      var returnUrl = to.fullPath;
      next("/?returnUrl=" + returnUrl); //返回登录授权，并且记录需要返回的路由
    }
  }
});

// keys() 从数组中创建一个数组迭代对象, 该对象包含了数组的键
Object.keys(formatDate).forEach(key => {
  // 返回可key值和下标对应的数组对象
  Vue.filter(key, formatDate[key]);
});
new Vue({
  Base,
  i18n,
  axios,
  router,
  store,
  render: h => h(App)
}).$mount("#app");
